package com.pounds.service.impl;

import com.pounds.mapper.HealthMapper;
import com.pounds.mapper.UserMapper;
import com.pounds.pojo.Health;
import com.pounds.service.HealthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HealthServiceImpl implements HealthService {

    @Autowired
    private HealthMapper healthMapper;

    @Autowired
    private UserMapper userMapper;

    enum HealthEnumError {
        Normal,
        DataFormatError,
        DataNotFoundError
    }

    @Override
    public List<Health> getHealthList() {
        return healthMapper.getHealthList();
    }

    @Override
    public String addHealth(int user, String morning, String noon, String night, String sleep, String need, String note) {
        if (user < 0) {
            return HealthEnumError.DataFormatError.toString();
        }
        String name = userMapper.selectUserName(user);
        if (name == null) {
            return HealthEnumError.DataNotFoundError.toString();
        } else if (name.isEmpty()) {
            return HealthEnumError.DataNotFoundError.toString();
        }
        healthMapper.addHealth(user, name, morning, noon, night, sleep, need, note);
        return HealthEnumError.Normal.toString();
    }

    @Override
    public String deleteHealth(int id) {
        List<Health> health = healthMapper.selectHealth(id);
        if (health == null) {
            return HealthEnumError.DataNotFoundError.toString();
        }
        healthMapper.deleteHealth(id);
        return HealthEnumError.Normal.toString();
    }

    @Override
    public String updateHealth(int id, int user, String morning, String noon, String night, String sleep, String need, String note) {
        if (user < 0 || id < 0) {
            return HealthEnumError.DataFormatError.toString();
        }
        String name = userMapper.selectUserName(user);
        if (name == null) {
            return HealthEnumError.DataNotFoundError.toString();
        } else if (name.isEmpty()) {
            return HealthEnumError.DataNotFoundError.toString();
        }
        healthMapper.updateHealth(id, user, name, morning, noon, night, sleep, need, note);
        return HealthEnumError.Normal.toString();
    }
}
